var page = 0
var search = ""
var name = ""
var good_img = ""
var item_id = 0
var channel_id = 0
var layer_index
var lock = true

// 获取淘宝商品信息
function get_good_new(page, search, name) {
    // console.log("page", page)
    var str = "&page="+page
    str += "&search="+search
    str += "&name="+name
    var login_do_return = $.ajax({
        type: "POST",
        url: "index.php?m=cloud&c=cloud_shop&a=get_good_new"+str,
        data: "",
        async: false
    }).responseText;
    // console.log(login_do_return)
    // console.log(JSON.parse(login_do_return))
    // return ""
    return JSON.parse(login_do_return)
}

// 获取子节点的自定义元素
function get_element_title(element, name) {
    for (var j in element.attributes) {
        var obj = element.attributes[j]
        if (obj.nodeName == name) {
            return obj.nodeValue
        }
    }
}

// 获取页面
function get_em(n) {
    var em =
    "<li class=\"go-share\" onClick= select_shop_data("+n.item_id+","+n.channel_id+") data-id="+n.item_id+">"+
        "<div class=\"div-img\">"+
            "<img class=\"img\" src="+n.image+" alt=\"\">"+
        "</div>"+
        "<div class=\"div-right\">"+
            "<div class=\"div-title\">"+n.name+"</div>"+
            "<div class=\"div-shop\" style=\"margin-bottom: 0.1rem;\">"+n.nick+
                "<span class=\"span\"></span>"+
            "</div>"+
            
            "<div class=\"div-price\" style=\"margin-bottom: 0.16rem;\">"+
                "<span class=\"span1\">￥</span>"+
                "<span class=\"span2\">"+(n.price-n.coupon_amount).toFixed(2)+"</span>"+
                "<del>¥"+n.price+"</del>"+
            "</div>"+
            
            "<div class=\"div1\" style=\"margin-bottom: 0.23rem;display: none\">"+
                "<div class=\"div1-1\">分享赚0元</div>"+
            "</div>"+

            "<div class=\"div2\" style=\"display: \">"+
                "<img class=\"div2-img\" src=\"public/cloud/statics/img/quan_bg.png\" alt=\"\">"+n.coupon_amount+"元券"+
            "</div>"+
        "</div>"+
    "</li>"
    return em
}

// 刷新当前页面函数
function refresh_interface(num) {
    var el = document.getElementsByName('title');
    // 设置竖滑动条置顶
    window.scrollTo(0,0)
	var len = el.length;
	for(var i = 0; i < len; ++i) {
        var obj = el[i]
        if (get_element_title(obj, "data_id") == num) {
            obj.className = "nav-a hover"
            search = get_element_title(obj, "data_search")
            name = get_element_title(obj, "data_name")
            good_img = get_element_title(obj, "data_img")
        }else {
            obj.className = "nav-a"
        }
    }
    var em = ""
    page = 0
    var good_new = get_good_new(page, search, name)
    for (var k in good_new) {
        em += get_em(good_new[k])
    }
    page += good_new.length
    // 清理已经加载的数据
    $("#quanwang-ul").html(em)
}

// 加载页面
function get_list() {
    if (lock) {
        lock = false
        var em = ""
        var good_new = get_good_new(page, search, name)
        for (var k in good_new) {
            em += get_em(good_new[k])
        }
        page += good_new.length
        // 添加数据
        $("#quanwang-ul").append(em)
        lock = true
    }
}

// 添加需要发送的商品群
function select_shop_data(_item_id, _channel_id) {
    item_id = _item_id
    channel_id = _channel_id
    if (document.addEventListener) {
        document.addEventListener('DOMMouseScroll', function(){return false} , false);
      }
    layer_index = layer.open({
		type: 2,
		title: '请选择需要发送商品信息的群或者朋友圈',
		maxmin: false,
        shadeClose: true, //点击遮罩关闭层
        skin:'demo-class',
        area:['320px','600px'],
        closeBtn:1,
        content: 'index.php?m=cloud&c=cloud_shop&a=show_sg_data',
        success: function(layero, index){},
        end: function () {},
    });
}

// 弹出提示框
function eject_title(str, fun) {
    if (fun) {
        layer.msg(str, function(){fun()})
    } else {
        layer.msg(str)
    }
}

// 发送群信息
function send_shop_data(_serial) {
    var str = "&item_id="+window.parent.item_id
    str += "&channel_id="+window.parent.channel_id
    str += "&good_img="+window.parent.good_img
    str += "&_serial="+_serial
    var index = layer.load(1, {
        shade: [0.1,'#000'] //0.1透明度的白色背景
    });
    var login_do_return = $.ajax({
        type: "POST",
        url: "index.php?m=cloud&c=cloud_shop&a=send_group_data"+str,
        data: "",
        async: false
    }).responseText;

    login_do_return = JSON.parse(login_do_return)
    if (login_do_return['state'] > 0) {
        layer.close(index)
        window.parent.eject_title("操作成功")
        layer.close(layer.index);
    } else {
        layer.close(index)
        window.parent.eject_title(login_do_return['msg'])
        if (login_do_return['state'] == -6) {
            location.href="index.php?m=cloud&c=cloud_login&a=login_main";
        }
    }
    
    window.parent.layer.close(window.parent.layer_index)
}


// 添加滚轮监听
$(document).on("scroll", function() {
    $(document).height()<=$(this).scrollTop()+$(window).height()+100?get_list():null
})

// 提前处理
$().ready(function() {
    refresh_interface(0)
});